#This code shows the results reported in tables 4, 5, and 6 in the main paper

if(!dir.exists("tabs")){dir.create("tabs")}

rm(list = ls())
library(lubridate)
library(foreign)
library(ggthemes)
library(stats)
library(lmtest)
library(sandwich)
library(quanteda)
library(stm)
library(stats)
library(ggpubr)
library(tidyverse)
library(tidytext)
gc()

# Reading data ------------------------------------------------------------
load("k40.RData")

beta = tidy(topic_model)
gamma = tidy(topic_model, matrix = "gamma")

meta = meta %>% mutate(document = article) %>% select(- article)

gamma = left_join(gamma, meta, by = "document")
gc()

gamma2 = gamma %>% 
  group_by(date, topic) %>% 
  summarise(avg = mean(gamma))

topics = tibble(topic = 1:40, name = c("communication", "assad", "isis", "temperature", "elections", 
                                       "weather", "foreign fighters", "united states", "announcements", "conspiracies and plots", 
                                       "election winners", "legislation", "terrorist attacks", "speeches", "victims", 
                                       "gulf", "accident", "media", "europe", "yemen", 
                                       "culture and sports", "capitulation", "economy", "diplomacy", "religion", 
                                       "transport", "natural resources", "national unity", "lebanon", "finance", 
                                       "bureaucracy", "education", "fighting terrorism", "Israel and Palestine", "ba'th party",
                                       "Iraq", "Turkey", "russia", "Iran", "regional politics"))

gamma2 = gamma2 %>% ungroup %>% left_join(topics)

gamma2 = gamma2 %>% 
  ungroup() %>% 
  group_by(date, topic) %>% 
  arrange(date, topic) %>% 
  ungroup()

gamma2 = gamma2 %>% 
  select(- topic) %>% 
  rename(topic = name) %>% 
  spread(topic, avg)

gamma2



# syrian revolution -------------------------------------------------------
t = Sys.time()
effect_rvln = estimateEffect(1:40 ~ rvln, stmobj = topic_model, meta = meta, uncertainty = "Global")
Sys.time() - t

effect_rvln = effect_rvln %>% tidy

topics = tibble(topic = 1:40, `Topic Label` = c("communication", "assad", "isis", "temperature", "elections", 
                                                "weather", "foreign fighters", "united states", "announcements", "conspiracies and plots", 
                                                "election winners", "legislation", "terrorist attacks", "speeches", "victims", 
                                                "gulf", "accident", "media", "europe", "yemen", 
                                                "culture and sports", "capitulation", "economy", "diplomacy", "religion", 
                                                "transport", "natural resources", "national unity", "lebanon", "finance", 
                                                "bureaucracy", "education", "fighting terrorism", "Israel and Palestine", "ba'th party",
                                                "Iraq", "Turkey", "Russia", "Iran", "regional politics"))

effect_rvln = effect_rvln %>% left_join(topics) %>% select(-topic) %>% rename(topic = `Topic Label`)


tibble(` ` = c("Uprisings (March 15, 2011)", "Intercept"),
       `Israel and Palestine` = c(paste0(round(effect_rvln$estimate[effect_rvln$topic == "Israel and Palestine" & effect_rvln$term == "rvln"], 3), " (", 
                                         round(effect_rvln$std.error[effect_rvln$topic == "Israel and Palestine" & effect_rvln$term == "rvln"], 3), ")"),
                                  paste0(round(effect_rvln$estimate[effect_rvln$topic == "Israel and Palestine" & effect_rvln$term == "(Intercept)"], 3), " (", 
                                         round(effect_rvln$std.error[effect_rvln$topic == "Israel and Palestine" & effect_rvln$term == "(Intercept)"], 3), ")")
                                  ),
       
       `Assad` = c(paste0(round(effect_rvln$estimate[effect_rvln$topic == "assad" & effect_rvln$term == "rvln"], 3), " (", 
                                       round(effect_rvln$std.error[effect_rvln$topic == "assad" & effect_rvln$term == "rvln"], 3), ")"),
                   paste0(round(effect_rvln$estimate[effect_rvln$topic == "assad" & effect_rvln$term == "(Intercept)"], 3), " (", 
                          round(effect_rvln$std.error[effect_rvln$topic == "assad" & effect_rvln$term == "(Intercept)"], 3), ")")
                   ),
       
       `Diplomacy` = c(paste0(round(effect_rvln$estimate[effect_rvln$topic == "diplomacy" & effect_rvln$term == "rvln"], 3), " (", 
                        round(effect_rvln$std.error[effect_rvln$topic == "diplomacy" & effect_rvln$term == "rvln"], 3), ")"),
                       paste0(round(effect_rvln$estimate[effect_rvln$topic == "diplomacy" & effect_rvln$term == "(Intercept)"], 3), " (", 
                              round(effect_rvln$std.error[effect_rvln$topic == "diplomacy" & effect_rvln$term == "(Intercept)"], 3), ")")
                       ),
       
       `Bureaucracy` = c(paste0(round(effect_rvln$estimate[effect_rvln$topic == "bureaucracy" & effect_rvln$term == "rvln"], 3), " (", 
                        round(effect_rvln$std.error[effect_rvln$topic == "bureaucracy" & effect_rvln$term == "rvln"], 3), ")"),
                        paste0(round(effect_rvln$estimate[effect_rvln$topic == "bureaucracy" & effect_rvln$term == "(Intercept)"], 3), " (", 
                               round(effect_rvln$std.error[effect_rvln$topic == "bureaucracy" & effect_rvln$term == "(Intercept)"], 3), ")"))
       ) %>% 
  xtable::xtable() %>% 
  xtable::print.xtable(file = "tabs/historic_topics.tex", include.rownames = F)






tibble(` ` = c("Uprisings (March 15, 2011)", "Intercept"),
       `Conspiracies and Plots` = c(paste0(round(effect_rvln$estimate[effect_rvln$topic == "conspiracies and plots" & effect_rvln$term == "rvln"], 3), " (", 
                                         round(effect_rvln$std.error[effect_rvln$topic == "conspiracies and plots" & effect_rvln$term == "rvln"], 3), ")"),
                                  paste0(round(effect_rvln$estimate[effect_rvln$topic == "conspiracies and plots" & effect_rvln$term == "(Intercept)"], 3), " (", 
                                         round(effect_rvln$std.error[effect_rvln$topic == "conspiracies and plots" & effect_rvln$term == "(Intercept)"], 3), ")")
       ),
       
       `National Unity` = c(paste0(round(effect_rvln$estimate[effect_rvln$topic == "national unity" & effect_rvln$term == "rvln"], 3), " (", 
                          round(effect_rvln$std.error[effect_rvln$topic == "national unity" & effect_rvln$term == "rvln"], 3), ")"),
                   paste0(round(effect_rvln$estimate[effect_rvln$topic == "national unity" & effect_rvln$term == "(Intercept)"], 3), " (", 
                          round(effect_rvln$std.error[effect_rvln$topic == "national unity" & effect_rvln$term == "(Intercept)"], 3), ")")
       ),
       
       `Terrorist Attacks` = c(paste0(round(effect_rvln$estimate[effect_rvln$topic == "terrorist attacks" & effect_rvln$term == "rvln"], 3), " (", 
                              round(effect_rvln$std.error[effect_rvln$topic == "terrorist attacks" & effect_rvln$term == "rvln"], 3), ")"),
                       paste0(round(effect_rvln$estimate[effect_rvln$topic == "terrorist attacks" & effect_rvln$term == "(Intercept)"], 3), " (", 
                              round(effect_rvln$std.error[effect_rvln$topic == "terrorist attacks" & effect_rvln$term == "(Intercept)"], 3), ")")
       ),
       
       `Fighting Terrorism` = c(paste0(round(effect_rvln$estimate[effect_rvln$topic == "fighting terrorism" & effect_rvln$term == "rvln"], 3), " (", 
                                round(effect_rvln$std.error[effect_rvln$topic == "fighting terrorism" & effect_rvln$term == "rvln"], 3), ")"),
                         paste0(round(effect_rvln$estimate[effect_rvln$topic == "fighting terrorism" & effect_rvln$term == "(Intercept)"], 3), " (", 
                                round(effect_rvln$std.error[effect_rvln$topic == "fighting terrorism" & effect_rvln$term == "(Intercept)"], 3), ")"))
) %>% 
  xtable::xtable() %>% 
  xtable::print.xtable(file = "tabs/new_topics.tex", include.rownames = F)


tibble(` ` = c("Uprisings (March 15, 2011)", "Intercept"),
       `Conspiracies and Plots` = c(paste0(round(effect_rvln$estimate[effect_rvln$topic == "Iran" & effect_rvln$term == "rvln"], 3), " (", 
                                           round(effect_rvln$std.error[effect_rvln$topic == "Iran" & effect_rvln$term == "rvln"], 3), ")"),
                                    paste0(round(effect_rvln$estimate[effect_rvln$topic == "Iran" & effect_rvln$term == "(Intercept)"], 3), " (", 
                                           round(effect_rvln$std.error[effect_rvln$topic == "Iran" & effect_rvln$term == "(Intercept)"], 3), ")")
       ),
       
       `National Unity` = c(paste0(round(effect_rvln$estimate[effect_rvln$topic == "Russia" & effect_rvln$term == "rvln"], 3), " (", 
                                   round(effect_rvln$std.error[effect_rvln$topic == "Russia" & effect_rvln$term == "rvln"], 3), ")"),
                            paste0(round(effect_rvln$estimate[effect_rvln$topic == "Russia" & effect_rvln$term == "(Intercept)"], 3), " (", 
                                   round(effect_rvln$std.error[effect_rvln$topic == "Russia" & effect_rvln$term == "(Intercept)"], 3), ")")
       ),
       
       `Terrorist Attacks` = c(paste0(round(effect_rvln$estimate[effect_rvln$topic == "gulf" & effect_rvln$term == "rvln"], 3), " (", 
                                      round(effect_rvln$std.error[effect_rvln$topic == "gulf" & effect_rvln$term == "rvln"], 3), ")"),
                               paste0(round(effect_rvln$estimate[effect_rvln$topic == "gulf" & effect_rvln$term == "(Intercept)"], 3), " (", 
                                      round(effect_rvln$std.error[effect_rvln$topic == "gulf" & effect_rvln$term == "(Intercept)"], 3), ")")
       ),
       
       `Fighting Terrorism` = c(paste0(round(effect_rvln$estimate[effect_rvln$topic == "Turkey" & effect_rvln$term == "rvln"], 3), " (", 
                                       round(effect_rvln$std.error[effect_rvln$topic == "Turkey" & effect_rvln$term == "rvln"], 3), ")"),
                                paste0(round(effect_rvln$estimate[effect_rvln$topic == "Turkey" & effect_rvln$term == "(Intercept)"], 3), " (", 
                                       round(effect_rvln$std.error[effect_rvln$topic == "Turkey" & effect_rvln$term == "(Intercept)"], 3), ")"))
) %>% 
  xtable::xtable() %>% 
  xtable::print.xtable(file = "tabs/intl_topics.tex", include.rownames = F)
